﻿////////////////////////////////////////////////////////////////////////////////
// Source Copyright (C) J.Fan
// @author      J.Fan
// @date        2013-01-19
// @revision    Revision in Version Control System
// @remark      
////////////////////////////////////////////////////////////////////////////////

using System;

namespace JsonConverter.Converter
{
    /// <summary>
    /// Convert DateTime value to string in Microsoft format,
    /// such as "\/Date(1358524800000+0800)\/"
    /// </summary>
    public class DateTimeMicrosoftConverter: IPrimitiveConverter
    {
        public const string PATTERN = @"""\/Date({0}{1:+00;-00}{2:00})\/""";

        public string Convert(object value)
        {
            TimeSpan offset = TimeZoneInfo.Local.BaseUtcOffset;
            return string.Format(PATTERN,
                DateTimeMillisecondConverter.ToJavaScriptMilliSeconds((DateTime) value),
                offset.Hours,
                offset.Minutes);
        }
    }
}
